module EventsHelper
  def event_text_description(event, opts = {})
    if event.nil?
      return ""
    end

    result_str = ""
    result_str << %(<strong>#{event.owner.username}</strong> is out )

    if event.today?
      result_str << %(<strong>today</strong>)
    elsif (event.begin_date - 1.day) == Date.today
      result_str << %(<strong>tomorrow</strong>)
    elsif opts.key?(:show_date) && opts[:show_date] == false
      result_str << %(<strong>#{event.begin_date.strftime("%A")}</strong>)
    else
      result_str << %(<strong>#{event.begin_date.strftime("%A, %B")}</strong> the )
      result_str << %(<strong>#{event.begin_date.day.to_s})
      result_str << case event.begin_date.day
      when 1 then "st"
      when 2 then "nd"
      when 3 then "rd"
      else        "th"
      end
      result_str << %(</strong>)
    end

    result_str << %( for <strong>)
    result_str << case event.duration_type
    when Event::PM        then "the afternoon"
    when Event::AM        then "the morning"
    when Event::FULL_DAY  then "the day"
    when Event::MULTI_DAY then pluralize(event.duration, "day")
    end

    
    result_str << %(</strong> for event: <strong><a href="#{event_path(event)}">#{event.name}</a></strong><br />)
    return raw(result_str)
  end

  def events_table(events, opts = {})
    
    default_fields = [:name,     :description, :owner,    :event_type, :begin_date, 
                      :end_date, :approved,    :approver, :creator,    :created_at,  :updated_at ]

    default_titles = { :name        => "Event Name",
                       :description => "Description",
                       :owner       => "Event Owner",
                       :event_type  => "Type of Event",
                       :begin_date  => "Begins on",
                       :end_date    => "Ends on",
                       :created_at  => "Created at",
                       :updated_at  => "Updated at",
                       :approved    => "Event Approved",
                       :creator     => "Event Creator" }

    default_links   = [:show, :edit]
    available_links = [:show, :edit, :approve, :destroy]

    fields_to_display = opts.has_key?(:fields) ? opts[:fields] : default_fields
     links_to_display = opts.has_key?(:links)  ? opts[:links]  : default_links
    titles_to_display = opts.has_key?(:titles) ? opts[:titles] : default_titles

    diff_fields = fields_to_display - default_fields
    diff_links  = links_to_display  - available_links

    if !diff_links.empty?
      raise "Called events_table with the following illegal link(s) #{diff_links.to_s}"
    elsif !diff_fields.empty?
      raise "Called events_table with the following illegal fields(s) #{diff_fields.to_s}"
    end

    content_tag(:table) do 
      
      # Display header
      content_tag(:tr) do
        fields_to_display.each do |field|
          content_tag(:th, titles_to_display[field]) 
        end
      end
    end
    events.each do |event|
      content_tag(:tr) do
        fields_to_display.each do |field|

          content_tag(:td) do
            case field
            when :name
              event.name
            when :description
              event.description
            when :owner
              link_to event.owner.username, event.owner
            when :event_type
              Event::EVENT_DURATION_NAMES[event.event_type]
            when :begin_date
              event.begin_date.to_s
            when :end_date
              event.end_date.to_s
            when :created_at
              event.created_at.to_formatted_s(:short)
            when :updated_at
              event.updated_at.to_formatted_s(:short)
            when :approved
              display_true_false_field(event.approved)
            when :creator
              link_to event.creator.username, event.creator
            end
          end

        end
      end
    end
  end
end
